#include "expquantitativemethodframe.h"
#include "ui_expquantitativemethodframe.h"
#include"delegate.h"

ExpQuantitativeMethodFrame::ExpQuantitativeMethodFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpQuantitativeMethodFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;

    ComboxDelegate *pComboxDelegate=new ComboxDelegate(this,3);
    ui->tableWidget_Element->setItemDelegateForColumn(1,pComboxDelegate);

    pComboxDelegate=new ComboxDelegate(this,4);
    ui->tableWidget_Quantitative->setItemDelegateForColumn(1,pComboxDelegate);
    pComboxDelegate=new ComboxDelegate(this,4);
    ui->tableWidget_Quantitative->setItemDelegateForColumn(2,pComboxDelegate);
    CheckBox *pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Quantitative->setItemDelegateForColumn(3,pCheckBox);

    pComboxDelegate=new ComboxDelegate(this,5);
    ui->tableWidget_Standard->setItemDelegateForColumn(1,pComboxDelegate);
    pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Standard->setItemDelegateForColumn(2,pCheckBox);

    LoadData();
}

ExpQuantitativeMethodFrame::~ExpQuantitativeMethodFrame()
{
    delete ui;
}

void ExpQuantitativeMethodFrame::LoadData()
{
    if(pExperiment==NULL) return;

    disconnect(ui->tableWidget_Element,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Element_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Quantitative,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Quantitative_itemChanged(QTableWidgetItem*)));
    disconnect(ui->tableWidget_Standard,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Standard_itemChanged(QTableWidgetItem*)));

    pExpMethod=pExperiment->GetExpMethodPtr();
    int row=0;
    ui->tableWidget_Element->setRowCount(row);
    QMap<double,QString> elements=pExperiment->GetQuantitativeElement(1);
    foreach(QString name,elements.values())
    {
        QuantitativePara para=pExpMethod->GetEleMethod(name).quantitativePara;
        int colomn=0;

        ui->tableWidget_Element->insertRow(ui->tableWidget_Element->rowCount());

        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(name);
        ui->tableWidget_Element->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(para.quantitativeMethod);
        ui->tableWidget_Element->setItem(row,colomn++,pItem);
        row++;
    }

    elements=pExperiment->GetQuantitativeElement(2);
    row=0;
    ui->tableWidget_Quantitative->setRowCount(row);
    foreach(QString name,elements.values())
    {
        QuantitativePara para=pExpMethod->GetEleMethod(name).quantitativePara;
        int colomn=0;

        ui->tableWidget_Quantitative->insertRow(ui->tableWidget_Quantitative->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(name);
        ui->tableWidget_Quantitative->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(para.functionCount);
        ui->tableWidget_Quantitative->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(para.weight);
        ui->tableWidget_Quantitative->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(para.bIsHalf));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Quantitative->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(para.limit));
        ui->tableWidget_Quantitative->setItem(row,colomn++,pItem);
        row++;
    }

    elements=pExperiment->GetQuantitativeElement(3);
    row=0;
    ui->tableWidget_Standard->setRowCount(row);
    foreach(QString name,elements.values())
    {
        QuantitativePara para=pExpMethod->GetEleMethod(name).quantitativePara;
        int colomn=0;

        ui->tableWidget_Standard->insertRow(ui->tableWidget_Standard->rowCount());
        QTableWidgetItem *pItem;
        pItem=new QTableWidgetItem;
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        pItem->setText(name);
        ui->tableWidget_Standard->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(para.weight);
        ui->tableWidget_Standard->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(para.bIsHalf));
        pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
        ui->tableWidget_Standard->setItem(row,colomn++,pItem);
        pItem=new QTableWidgetItem;
        pItem->setText(QString::number(para.limit));
        ui->tableWidget_Standard->setItem(row,colomn++,pItem);
        row++;
    }

    connect(ui->tableWidget_Element,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Element_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Quantitative,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Quantitative_itemChanged(QTableWidgetItem*)));
    connect(ui->tableWidget_Standard,SIGNAL(itemChanged(QTableWidgetItem*)),
               this,SLOT(on_tableWidget_Standard_itemChanged(QTableWidgetItem*)));
}

void ExpQuantitativeMethodFrame::on_tableWidget_Element_itemChanged(QTableWidgetItem *item)
{
    if(item->column()==0) return;
    QString name=ui->tableWidget_Element->item(item->row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);
    QString type=item->text();
    eleMethod.quantitativePara.quantitativeMethod=type;
    pExpMethod->SetMethod(name,eleMethod);

    LoadData();
}

void ExpQuantitativeMethodFrame::on_tableWidget_Quantitative_itemChanged(QTableWidgetItem *item)
{
    QString name=ui->tableWidget_Quantitative->item(item->row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);

    QString text=ui->tableWidget_Quantitative->item(item->row(),1)->text();
    eleMethod.quantitativePara.functionCount=text;
    text=ui->tableWidget_Quantitative->item(item->row(),2)->text();
    eleMethod.quantitativePara.weight=text;
    text=ui->tableWidget_Quantitative->item(item->row(),3)->text();
    eleMethod.quantitativePara.bIsHalf=text.toInt();
    text=ui->tableWidget_Quantitative->item(item->row(),4)->text();
    eleMethod.quantitativePara.limit=text.toDouble();
    pExpMethod->SetMethod(name,eleMethod);
    eleMethod=pExpMethod->GetEleMethod(name);
}

void ExpQuantitativeMethodFrame::on_tableWidget_Standard_itemChanged(QTableWidgetItem *item)
{
    QString name=ui->tableWidget_Standard->item(item->row(),0)->text();
    EleMethod eleMethod=pExpMethod->GetEleMethod(name);

    QString text=ui->tableWidget_Standard->item(item->row(),1)->text();
    eleMethod.quantitativePara.weight=text;
    text=ui->tableWidget_Standard->item(item->row(),2)->text();
    eleMethod.quantitativePara.bIsHalf=text.toInt();
    text=ui->tableWidget_Standard->item(item->row(),3)->text();
    eleMethod.quantitativePara.limit=text.toDouble();

    pExpMethod->SetMethod(name,eleMethod);
}
